<?php

class Controller_Admin_Product extends Controller_Admin_Template {
    
    private $model;
    
    public function __construct() 
    {
        parent::__construct();
        $this->model = new Model_Product();
    }


    public function actionList() 
    {
        $product_model = new Model_Product();
        $this->template->title = 'Products list';
        $this->template->content = new View('admin/products/list');
        $this->template->content->product_list = $product_model->getProductList();
    }
    
    public function actionEdit($id = NULL)
    {
        $size_model = new Model_Size();
        $sizes = $size_model->getSizesForProduct($id);
        
        $color_model = new Model_Color();
        $colors = $color_model->getColorsForProduct($id);
        
        $this->processForm($sizes, $colors, array(), $id);
        
    }
    
    public function actionAdd()
    {
        $this->template->title = 'Adding new product';
        $this->processForm();
    }
    
    private function processForm($sizes = array(), $colors = array(), $images = array(), $id = NULL)
    {
        $category_model = new Model_Category();
        $categories = $category_model->getCategoriesForProductSelect();
        
        $form = new Form("product_form", 'Adding new product', '');
        $form->text('name', 'Product name')->attribute('size', 45)
                ->rule('maxLength', 45)->rule('required');
        $form->select('category_id', $categories, 'Select category')->rule('required');
        $form->textarea('short_description', 'Short description')->attribute('cols', 80)
                ->attribute('rows', 5)->rule('maxLength', 255);
        $form->textarea('description', 'Description')->attribute('cols', 80)
                ->attribute('rows', 10)->rule('maxLength', 1024);
        $form->text('count', 'Product count')->attribute('size', 24)
                ->rule('required')->rule('integer')->rule('maxLength', 6);
        $form->text('price', 'Product price')->attribute('size', 24)
                ->rule('required')->rule('float')->rule('maxLength', 9);
        $form->submit('submit', 'Save data');
        
        if ($id)
        {
            $this->template->title = 'Editing product';
            $values = $this->model->getProduct($id);
            $form->loadValues($values);
        }
        
        if (Request::$method == 'POST')
        {
            $is_valid = $form->validate();
            $values = $form->getValues();
            if ($is_valid)
            {
                if ($id)
                {
                    Database::instance()->update('products', $values, array('id' => $id));
                }
                else
                {
                    Database::instance()->insert('products', $values);
                }
            }
            else
            {
                $form->loadValues($values);
            }
        }
        $vars = array(
            'form' => $form,
            'sizes' => $sizes,
            'color' => $colors
        );
        $this->template->content = new View('admin/products/add', $vars);        
    }
}